#include "settingdialog.h"
#include "ui_settingdialog.h"
#include "sys_config.h"
#include <QIntValidator>
#include "mainwindow.h"

extern DeviceData dev_data;

SettingDialog::SettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingDialog)
{
    ui->setupUi(this);
    ui->modbus_addr_label->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    ui->modbus_addr_edit->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    ui->control_mode_label->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    ui->control_mode_com->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    ui->cell_mode_label->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    ui->cell_model_com->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    ui->tip_label->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    ui->save_btn->setFont(QFont(SYS_FONT_TYPE, FONT_SIZE_20));
    QIntValidator *aIntvalidator = new QIntValidator;
    aIntvalidator->setRange(1,255);
    ui->modbus_addr_edit->setValidator(aIntvalidator);
    timer = new QTimer(this);
//    if (dev_data.modbus_addr <= 0) {
//        dev_data.modbus_addr = 1;
//    }
    if (dev_data.setting_modbus_addr <= 0) {
        dev_data.setting_modbus_addr = 8;
    }
    QString str = QString::number(dev_data.setting_modbus_addr);
    ui->modbus_addr_edit->setText(str);
    ui->control_mode_com->setCurrentIndex(dev_data.setting_control_mode);
    switch(dev_data.setting_cell_model) {
    case 10: ui->cell_model_com->setCurrentIndex(0); break;
    case 20: ui->cell_model_com->setCurrentIndex(1); break;
    case 30: ui->cell_model_com->setCurrentIndex(2); break;
    case 40: ui->cell_model_com->setCurrentIndex(3); break;
    default: ui->cell_model_com->setCurrentIndex(0); break;
    }

#if (EN_LANGUAGE_ENGLISH == 1)
    setWindowTitle(tr("Settings"));
    ui->save_btn->setText(tr("Save"));
    ui->tip_label->setText(tr("Tip:"));
    ui->cell_mode_label->setText(tr("Cell model"));
    ui->modbus_addr_label->setText(tr("Modbus addr(DEC)"));
    ui->control_mode_label->setText(tr("Control mode"));
#endif

//    connect(timer, &QTimer::timeout, this, &SettingDialog::timer_timeout);

}

SettingDialog::~SettingDialog()
{
    delete ui;
}

void SettingDialog::on_save_btn_clicked()
{
    int32_t addr;

    if (timer->isActive()) {
        timer->stop();
    }
#if (EN_LANGUAGE_ENGLISH == 1)
    ui->tip_label->setText(tr("Tip:"));
#else
    ui->tip_label->setText(tr("提示:"));
#endif
    addr = ui->modbus_addr_edit->text().toInt();
    if (addr < 1 || addr > 255) {
#if (EN_LANGUAGE_ENGLISH == 1)
        ui->tip_label->setText(tr("Tip: Modbus addr range: [1~255]"));
#else
        ui->tip_label->setText(tr("提示: modbus addr range: [1~255]"));
#endif
        return;
    }
    dev_data.setting_modbus_addr = addr;
    dev_data.setting_control_mode = ui->control_mode_com->currentIndex();
    switch (ui->cell_model_com->currentIndex()) {
    case 0: dev_data.setting_cell_model = 10; break;
    case 1: dev_data.setting_cell_model = 20; break;
    case 2: dev_data.setting_cell_model = 30; break;
    case 3: dev_data.setting_cell_model = 40; break;
    default: dev_data.setting_cell_model = 20; break;
    }
    this->close();

    return ;
    dev_data.setting_flag = 1;

    #if (EN_LANGUAGE_ENGLISH == 1)
    ui->tip_label->setText(tr("Tip: Saving parameter settings..."));
    #else
    ui->tip_label->setText(tr("提示: 设置参数保存中..."));
    #endif
    timer->start(3000);
}

void SettingDialog::timer_timeout()
{
#if (EN_LANGUAGE_ENGLISH == 1)
    if (dev_data.setting_flag == 2) {
        ui->tip_label->setText(tr("Tip: Parameter setting successful!"));
    } else {
        ui->tip_label->setText(tr("Tip: Parameter setting failed!"));
    }
#else
    if (dev_data.setting_flag == 2) {
        ui->tip_label->setText(tr("提示: 参数设置成功!"));
    } else {
        ui->tip_label->setText(tr("提示: 参数设置失败!"));
    }
#endif
    dev_data.setting_flag = 0;
    if (timer->isActive()) {
        timer->stop();
    }
}

